/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ex3ldap;

import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

/**
 *
 * @author Patrick Füssinger
 * bis 28.11.2011
 * ITB5_2
 * Exercise 3 - LDAP
 * 
 * Diese Klasse stellt eine Verbindung zu meinem eigenen Ldap-Verzeichnis her
 * und liest die Daten eines Users oder von Devices aus.
 * 
 * 
 * Für die Registrierung verwende ich den Admin als uid und das default passwort secret
 */
public class LdapAccess {

    private static Hashtable _contextProperties = new Hashtable();

    private static void initHashtable() {
        _contextProperties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        _contextProperties.put(Context.PROVIDER_URL, "ldap://localhost:10389/");
        _contextProperties.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
        _contextProperties.put(Context.SECURITY_CREDENTIALS, "secret");
    }

    public void searchFamiliyMembersBySurname(String sn) throws NamingException {

        
        InitialDirContext iDC = new InitialDirContext(_contextProperties);

        //Setze den Surmane
        String surname = "sn=" + sn;
        SearchControls cls = new SearchControls();

        //Setz den Verzeichnisbaum, wo sicher der FamilyMember befindet
        String base = "ou=familyMembers,ou=users,o=mojo";

        //Suchen und das Ergebnis ausgeben
        NamingEnumeration<SearchResult> searchRes = iDC.search(base, surname, cls);
        
        while (searchRes.hasMore()) {
            
            Attributes as = searchRes.next().getAttributes();
            NamingEnumeration<? extends Attribute> temp = as.getAll();
            
            while (temp.hasMore()) {
                Attribute current = temp.next();
                System.out.println(current);
            }
            
            System.out.println("----------------------->>>");
        }
    }

    public void searchStaffBySurname(String sn) throws NamingException {

        InitialDirContext iDC = new InitialDirContext(_contextProperties);

        //Setze den Surmane
        String surname = "sn=" + sn;
        SearchControls cls = new SearchControls();

        //Setzen den Verzeichnisbaum, wo sich der Staff befindet
        String base = "ou=staff,ou=users,o=mojo";

        //Suchen und das Ergebnis ausgeben
        NamingEnumeration<SearchResult> searchRes = iDC.search(base, surname, cls);
        
        while (searchRes.hasMore()) {
            
            Attributes as = searchRes.next().getAttributes();
            NamingEnumeration<? extends Attribute> temp = as.getAll();
            
            while (temp.hasMore()) {
                Attribute current = temp.next();
                System.out.println(current);
            }
            
            System.out.println("----------------------->>>");
        }
    }

    public void searchDevicesByCommonName(String cn) throws NamingException {

        InitialDirContext iDC = new InitialDirContext(_contextProperties);

        //Setze den Surmane
        String surname = "cn=" + cn;
        SearchControls cls = new SearchControls();
        
        //Setzen den Verzeichnisbaum, wo sich die Devices befinden
        String base = "ou=devices,o=mojo";

        //Suchen und das Ergebnis ausgeben
        NamingEnumeration<SearchResult> searchRes = iDC.search(base, surname, cls);
        
        while (searchRes.hasMore()) {
            
            Attributes as = searchRes.next().getAttributes();
            NamingEnumeration<? extends Attribute> temp = as.getAll();
            
            while (temp.hasMore()) {
                Attribute current = temp.next();
                System.out.println(current);
            }
            
            System.out.println("----------------------->>>");
        }

    }

    /**
     * Main
     * @param args 
     */
    public static void main(String[] args) {
        try {
            LdapAccess oLdapAccess = new LdapAccess();
            initHashtable();

            oLdapAccess.searchFamiliyMembersBySurname("Füssinger");
            oLdapAccess.searchStaffBySurname("Browning");
            oLdapAccess.searchDevicesByCommonName("HP Printer");
        } catch (NamingException ex) {
            Logger.getLogger(LdapAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
